package student.mindmap.core.model;

import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;

public class MindMapTreeCall extends TreeCell<String> {
    private final HBox graphic = new HBox();
    private final Label label = new Label();
    private final Pane indentPane = new Pane();

    public MindMapTreeCall() {
        graphic.setAlignment(Pos.CENTER_LEFT);
        graphic.getChildren().addAll(indentPane, label);

        // 设置样式
        label.setStyle("-fx-font-family: 'Microsoft YaHei'; -fx-font-size: 14;");
        indentPane.setStyle("-fx-background-color: transparent;");
    }

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            setGraphic(null);
        } else {
            // 根据层级计算缩进
            int depth = getTreeItemDepth(getTreeItem());
            indentPane.setPrefWidth(depth * 20); // 每级缩进20px
            label.setText(item);
            setGraphic(graphic);
        }
    }

    private int getTreeItemDepth(TreeItem<?> item) {
        int depth = 0;
        TreeItem<?> parent = item.getParent();
        while (parent != null) {
            depth++;
            parent = parent.getParent();
        }
        return depth;
    }
}